package com.byxy.computer.student.service;

import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.byxy.computer.student.dao.StudentDao;
import com.byxy.computer.student.entity.Student;

/**
 * @author Daniel
 * @E-Mail iva0512@gmail.com
 * @Date 2016年9月25日
 */
@Service
@Transactional
public class StudentServiceImpl implements StudentService {

	@Autowired
	private StudentDao studentDao;

	@Override
	public Student save(Student std) {
		return studentDao.save(std);
	}

	@Override
	public List<Student> listAll() {
		return studentDao.findAll();
	}

	@Override
	public Student get(Student std) {
		if (std != null)
			return studentDao.findOne(std.getUid());
		return null;
	}

	@Override
	public List<Student> save(List<Student> std) {
		return studentDao.save(std);
	}

	@Override
	public void deleteAll() {
		studentDao.deleteAllInBatch();
	}

	@Override
	public void sayGood(Student std) {
		Student s = studentDao.findOne(std.getUid());
		if (s != null) {
			if (s.getGood() == null)
				s.setGood(0);
			if (s.getStats() != null)
				s.setGood(s.getGood().intValue() + 1);
			studentDao.save(s);
		}
	}

	@Override
	public void sayBad(Student std) {
		Student s = studentDao.findOne(std.getUid());
		if (s != null) {
			if (s.getGood() == null)
				s.setGood(0);
			if (s.getStats() != null)
				s.setGood(s.getGood().intValue() - 1);
			studentDao.save(s);
		}
	}

	@Override
	public void resetLesson(Student std) {
		Student s = studentDao.findOne(std.getUid());
		if (s != null) {
			s.setGood(0);
			s.setStats(null);
			s.setComputername(null);
			s.setFilesize(0);
			studentDao.save(s);
		}
	}

	@Override
	public Student sign(Student std) {
		Student s = studentDao.findOne(std.getUid());
		if (s != null) {
			if (!"C".equals(s.getStats()))
				s.setStats("√");
			s.setComputername(std.getComputername());
			return studentDao.save(s);
		}
		return null;
	}

	@Override
	public Student upload(Student std, String filepath, int size) {
		Student s = studentDao.findOne(std.getUid());
		if (s != null) {
			s.setHomework(filepath);
			s.setFilesize(size / 1024);
			return studentDao.save(s);
		}
		return null;
	}

	@Override
	public void playTruant(Student student) {
		Student s = studentDao.findOne(student.getUid());
		if (s != null) {
			s.setStats("K");
			studentDao.save(s);
		}
	}

	@Override
	public void beLate(Student student) {
		Student s = studentDao.findOne(student.getUid());
		if (s != null) {
			s.setStats("C");
			studentDao.save(s);
		}

	}

	@Override
	public void holiday(Student student) {
		Student s = studentDao.findOne(student.getUid());
		if (s != null && (s.getStats() == null || "".equals(s.getStats()))) {
			s.setStats("S");
			studentDao.save(s);
		}
	}

	@Override
	public void sick(Student student) {
		Student s = studentDao.findOne(student.getUid());
		if (s != null && (s.getStats() == null || "".equals(s.getStats()))) {
			s.setStats("B");
			studentDao.save(s);
		}

	}
}
